package nero.yncc.com.createart.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterTaozhuang;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanToazhuang;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.ShareUtil;

/**
 * Created by nero on 17-7-6.
 */

@ContentView(R.layout.activity_zuhe)
public class ActivityTaozhuang extends BaseActivity {
    @ViewInject(R.id.lv_list)
    MyRecyclerView lv_list;

    @ViewInject(R.id.btn_addShopCar)
    Button btn_addShopCar;
    @ViewInject(R.id.icon_shopcar)
    IconView icon_shopcar;
    @ViewInject(R.id.icon_share)
    IconView icon_share;

    @ViewInject(R.id.tv_all_sheng)
            TextView tv_all_sheng;
    @ViewInject(R.id.tv_allNum)
            TextView tv_allNum;
    @ViewInject(R.id.tv_money_all)
            TextView tv_money_all;

    String id;

    ArrayList<BeanToazhuang> arrayList;
    AdapterTaozhuang adapterTaozhuang;

    @Override
    protected void initData() {
        super.initData();
        arrayList = new ArrayList<>();
        id = getIntent().getStringExtra("id");
        adapterTaozhuang = new AdapterTaozhuang(mContext, arrayList);
        lv_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        lv_list.setAdapter(adapterTaozhuang);

    }

    @Override
    protected void initView() {
        super.initView();
        setTitleBarTitle("促销");
    }

    @Override
    protected void initListener() {
        super.initListener();
        btn_addShopCar.setOnClickListener(this);
        icon_shopcar.setOnClickListener(this);
        icon_share.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id) {
            case R.id.btn_addShopCar:
                addShopCar();
                break;
            case R.id.icon_shopcar:
                startActivity(new Intent(mContext, ActivityShopcar.class));
                break;
            case R.id.icon_share:
                ShareUtil.share(this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void getHttpData() {
        super.getHttpData();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("goods_id", id);
        new HttpUtils().post(this, Constants.API_ZUHE, hashMap, null, HttpUtils.TYPE_ARRAY, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                JSONArray jsonArray = (JSONArray) json;
                for (int a = 0; a < jsonArray.length(); a++) {
                    try {
                        BeanToazhuang beanToazhuang = JSON.parseObject(jsonArray.getString(a), BeanToazhuang.class);
                        for (GodBean godBean : beanToazhuang.getGoods()) {
                            if (godBean.getGoods().getStore_count().length() >= 1) {
                                godBean.getGoods().setNum(1);
                            }
                        }
                        arrayList.add(beanToazhuang);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                adapterTaozhuang.notifyDataSetChanged();

            }

            @Override
            public void fail(String failMsg) {

            }
        });
    }

    private void addShopCar() {
        if (num <= 0) {
            showToast("请先选择商品");
            return;
        }
        showProgress();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", id);
        hashMap.put("user_id", userBean.getUser_id());
        hashMap.put("content", strContent);

        new HttpUtils().post(this, Constants.API_TAOZHUANGADDCAR, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                showToast(msg);
                hideProgress();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                hideProgress();
            }
        });
    }

    double price = 0;
    double price_market = 0;
    String strContent = "";
    int num = 0;

    public void setGuigeMoney() {
//        {"goods_id": 101,"spec_list": [{"spec_id": 35,"item_id": 155}], "price": 100,"market_price": 250, "number": 1}


        strContent = "";
        StringBuilder strs = new StringBuilder();
        strs.append("[");
        price = 0;
        num = 0;
        for (BeanToazhuang beanToazhuang : arrayList) {
            ArrayList<GodBean> arrayList = beanToazhuang.getGoods();
            for (GodBean godBean : arrayList) {
                GodBean.Goods goods = godBean.getGoods();
                StringBuilder stringBuilder_cache = new StringBuilder();

                if (goods.isChoose() && goods.getNum() > 0) {
                    num+=goods.getNum();
                    stringBuilder_cache.append("{");

                    //选中了的商品 且有数量的商品
                    stringBuilder_cache.append("\"goods_id\": " + goods.getGoods_id() + ",\"spec_list\": [");
                    ArrayList<GodBean.SpecAttr> arrayList_spec = goods.getGoods_spec_list();
                    for (GodBean.SpecAttr specAttr : arrayList_spec) {
                        if (!TextUtils.isEmpty(specAttr.getChooseAttrId())) {
                            stringBuilder_cache.append("{\"spec_id\": ");
                            stringBuilder_cache.append(specAttr.getSpec_id());
                            stringBuilder_cache.append(",\"item_id\":");
                            stringBuilder_cache.append(specAttr.getChooseAttrId());
                            stringBuilder_cache.append("},");
                        }
                    }
                    if (stringBuilder_cache.toString().endsWith(",")) {
                        strs.append(stringBuilder_cache.toString(), 0, stringBuilder_cache.length() - 1);
                    } else {
                        strs.append(stringBuilder_cache.toString());
                    }
                    strs.append("],\"price\":");
                    strs.append(goods.getShop_price());
                    strs.append(",\"market_price\":");
                    strs.append(goods.getMarket_price());
                    strs.append(", \"number\":");
                    strs.append(goods.getNum());
                    strs.append("},");
//                            , "price": 100,"market_price": 250, "number": 1}

                    price += goods.getNum() * Double.parseDouble(goods.getShop_price());
                    price_market+=goods.getNum() * Double.parseDouble(goods.getMarket_price());
                }

            }
        }
        String endStr = strs.toString();
        if (endStr.endsWith(",")) {
            endStr = endStr.substring(0, endStr.length() - 1);
        }

        endStr += "]";
        showLog(endStr);
        strContent = endStr;
        tv_money_all.setText("￥" + price);
        tv_all_sheng.setText("立省￥" + (price_market-price));
        tv_allNum.setText(num+"件商品");

    }
}
